package com.gzhu.knowledgeAdmin.controller;



import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.common.vo.*;
import com.gzhu.knowledgeAdmin.service.HistoryRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


/**
 * 模块总览
 * @author zhangq
 * @date 2023/9/30 16:11
 */
@RestController
@CrossOrigin
@RequestMapping("/overview")
public class OverviewController {

    @Autowired
    private HistoryRecordService historyRecordService;
    //获取登录用户信息
    UserInfoUtils userInfoUtils = new UserInfoUtils();
    /**
     * 外呼状态分布
     * @return 外呼类型个数
     */
    @GetMapping("/serNum-list")
    public R findSerNumberNum(){
        List<Integer> result= historyRecordService.findSerNumberNum(userInfoUtils.getCurrentUser().getGsid());
        return R.ok(result);
    }


    /**
     * 对话时长分布
     * @return 统计历史服务记录中各项时间差的个数
     */
    @GetMapping("/timeCut-list")
    public R findTimeCut(){
        List<TimeVo> result= historyRecordService.findTimeCut(userInfoUtils.getCurrentUser().getGsid());
        return R.ok(result);
    }


    /**
     * 对话轮次分布
     * @return 任务结果记录对话轮次
     */
    @GetMapping("/diaNum-list")
    public R findDiaNum(){
        List<DialogueVo> result= historyRecordService.findDiaNum(userInfoUtils.getCurrentUser().getGsid());

        return R.ok(result);
    }

    /**
     * 短信点击跟踪
     * @return 获取短信短链发送成功次数和发送总数
     */
    @GetMapping("/messageNum-list")
    public R findMCNum(){
        List<MessageVo> result= historyRecordService.findMCNum(userInfoUtils.getCurrentUser().getGsid());
        return R.ok(result);
    }

    /**
     *
     * @return 获取外呼到达数据
     */
    @GetMapping("/arriveNum-list")
    public R findArriveNum(){
        List<ArriveVo> result= historyRecordService.findArriveNum(userInfoUtils.getCurrentUser().getGsid());
        return R.ok(result);
    }


    /**
     * 意向分布图
     * @return 意向标签数据
     */
    @GetMapping("/intentNum-list")
    public R findIntentNum(){
        List<IntentVo> result= historyRecordService.findIntentNum(userInfoUtils.getCurrentUser().getGsid());

        return R.ok(result);
    }



    /**
     * 相关意向路线分布
     * @param F_YXBQ 意向标签
     * @return 获取线路数据
     */
    @GetMapping("/routeNum-list")
    public R findRouteNum(String F_YXBQ) {
        List<RouteVo> result = historyRecordService.findRouteNum(userInfoUtils.getCurrentUser().getGsid(), F_YXBQ);

        return R.ok(result);
    }



    /**
     * 标签采集表
     * 分页查询
     * @param params 分页
     * @return 获取标签
     * @throws Exception
     */
    @GetMapping("/label-list")
    public Map<String, Object> findLabel(String params) throws Exception {

        ObjectMapper objectMapper = new ObjectMapper();
        // 解析 JSON
        JsonNode jsonNode = objectMapper.readTree(params);
        // 获取值
        int current =jsonNode.get("current").asInt();//页码
        int size = jsonNode.get("size").asInt();//页面大小
        Map<String, Object> mapValue = new HashMap<>();
        mapValue.put("F_GSID",userInfoUtils.getCurrentUser().getGsid());
        mapValue.put("pageCurrent",(current-1)*size);
        mapValue.put("pageSize",size);

        //查询数据量
        int total= historyRecordService.findLabelNum(userInfoUtils.getCurrentUser().getGsid());

        Map<String, Object> map = new HashMap<>();
        List<LabelVo> result = historyRecordService.findLabel(mapValue);
        map.put("message", "ok");
        map.put("code", ErrorCode.SUCCESS.getCode());
        map.put("data", result);
        map.put("total", total);
        return map;
    }
}
